$(function(){
	var firstdepart = $('.first-depart'),
		seconddepart = $('.second-depart'),
		firstForm = $('#first-depart'),
		secondForm = $('#second-depart'),
		imgModal = $('#imgModal');


	// 点编辑
	$('.editate').on('click',function(){
		var id = $(this).data('id');
		var listr = '';
		$('#eidateId').val(id);
		imgModal.removeClass('hide');
		firstdepart.removeClass('hide');
		seconddepart.addClass('hide');
		var committeeid = $(this).data('committee');
		var reseauid = $(this).data('reseau');

		$.each($('#neighborhood1 .option'),function(i,val){
			if ($(val).data('value') == committeeid) {
				$(val).addClass('selected');
				$(val).siblings().removeClass('selected');
				$('#neighborhood1').siblings('input').val(committeeid);
				$('#neighborhood1').siblings('.show-option').text($(val).find('span').text());
			}
		})
		$.ajax({
			url: '/Admin/User/getAllReseau',
			type:'post',
			dataType:'json',
			data:{id:committeeid},
			success:function(json){
				console.log(json)
		        $.each(json,function(i,val){
		    		listr += '<div class="option" data-value="'+val.id+'" data-url=""><span>'+val.nbh_name+'</span></div>';
					$('#grid1').html(listr);
		        })
		        
		        $.each($('#grid1 .option'),function(i,val){
					if ($(val).data('value') == reseauid) {
						$(val).addClass('selected');
						$(val).siblings().removeClass('selected');
						$('#grid1').siblings('input').val(reseauid);
						$('#grid1').siblings('.show-option').text($(val).find('span').text());
					}
				})
			},
			error:function(json){

			}
		})
	})

		// 联动

	$('body').on('mousedown','#neighborhood1 .option',function(){
		var id = $(this).data('value');
		var listr = '';
		$('#grid1').html(listr);
		$('#grid1').siblings('input').val('');
		$('#grid1').siblings('.show-option').text('');


		$.ajax({
			url: '/Admin/User/getAllReseau',
			type:'post',
			dataType:'json',
			data:{id:id},
			success:function(json){
				console.log(json)
		        $.each(json,function(i,val){
		    		listr += '<div class="option" data-value="'+val.id+'" data-url=""><span>'+val.nbh_name+'</span></div>';
					$('#grid1').html(listr);
		        })
		        
		        
			},
			error:function(json){

			}
		})
	})

			// 联动

	$('body').on('mousedown','#neighborhood .option',function(){
		var id = $(this).data('value');
		var listr = '';
		$('#grid').html(listr);
		$('#grid').siblings('input').val('');
		$('#grid').siblings('.show-option').text('');


		$.ajax({
			url: '/Admin/User/getAllReseau',
			type:'post',
			dataType:'json',
			data:{id:id},
			success:function(json){
				console.log(json)
		        $.each(json,function(i,val){
		    		listr += '<div class="option" data-value="'+val.id+'" data-url=""><span>'+val.nbh_name+'</span></div>';
					$('#grid').html(listr);
		        })
		        
		        
			},
			error:function(json){

			}
		})
	})
	// 提交
		firstForm.validate();
		firstForm.on('submit',function(){
			try {
				if (firstForm.valid()) {
					firstForm.submitForm();
				}

			}catch(e){
				console.log(e)
			}

			return false;
		})


 	function triggerSelect() {
        var val = $(this).val(),
            selectElem = $(this).siblings('.select-options'),
            showOption = $(this).siblings('.show-option');

        selectElem.find('.option ').each(function(n, elem) {
            if ($(this).data('value') == val) {
                $(this).addClass('selected').siblings('.option').removeClass('selected');
                showOption.html($(this).find('span').text());
            }
        }) 
    }

	var firstTerrace = $('#firstTerrace'),
		neighborpower = $('#neighborpower'),
		neighborperson = $('#neighborperson'),
		firstPerson = $('#firstPerson'),
		secondPerson = $('#secondPerson'),
		threePerson = $('#threePerson'),
		first_k = $('.first-k'),
		second_k = $('.second-k'),
		three_k = $('.three-k');


	// 点权限
	$('.power').on('click',function(){
		var id = $(this).data('id');
		$('#powerId').val(id);
		imgModal.removeClass('hide');
		firstdepart.addClass('hide');
		seconddepart.removeClass('hide');
		threePerson.attr('name','');	
		var listr = '';


		var committeeid = $(this).data('committee');
		var reseauid = $(this).data('reseau');
		var usergroupid = $(this).data('usergroup');

		var inputElem = $('#power');
		inputElem.val(usergroupid).trigger('change');
		triggerSelect.call(inputElem);
		var selectedOpt =  $('#firstTerrace').find('.selected');
 

		if (selectedOpt.hasClass('second-k')) {
			console.log(1)

			neighborpower.addClass('hide'); 
			neighborperson.removeClass('hide');
			threePerson.attr('name','committee');
			firstPerson.attr('name','');
			secondPerson.attr('name',''); 
		}else if (selectedOpt.hasClass('three-k')){
			console.log(2)

			neighborpower.addClass('hide');
			neighborperson.removeClass('hide');
			threePerson.attr('name','committee');
			firstPerson.attr('name','');
			secondPerson.attr('name','');
		}else if (selectedOpt.hasClass('first-k')){
			console.log(3)
			neighborpower.removeClass('hide');
			neighborperson.addClass('hide');
			$('#firstTerrace').siblings('input').val('3');
			firstPerson.attr('name','committee');
			secondPerson.attr('name','reseau');
			threePerson.attr('name','');
		}else {
			$('.first-k').addClass('selected');
			$('.first-k').siblings().removeClass('selected');
			$('#firstTerrace').siblings('input').val('3');
			neighborpower.removeClass('hide');
			neighborperson.addClass('hide');

			firstPerson.attr('name','committee');
			secondPerson.attr('name','reseau');
			threePerson.attr('name','');
		}

		$.each($('#neighborhood3 .option'),function(i,val){

			if ($(val).data('value') == committeeid) {
				if (committeeid != 0) {
					$(val).addClass('selected');
					$(val).siblings().removeClass('selected');
					$('#neighborhood3').siblings('input').val(committeeid);
					$('#neighborhood3').siblings('.show-option').text($(val).find('span').text());
				}else if (committeeid == 0){
					$(val).addClass('selected');
					$(val).siblings().removeClass('selected');
					$('#neighborhood3').siblings('input').val('');
					$('#neighborhood3').siblings('.show-option').text($(val).find('span').text());
				}
				return false;
			}
		})

		$.each($('#neighborhood2 .option'),function(i,val){
			if ($(val).data('value') == committeeid) {

				if (committeeid != 0) {
					$(val).addClass('selected');
					$(val).siblings().removeClass('selected');
					$('#neighborhood2').siblings('input').val(committeeid);
					$('#neighborhood2').siblings('.show-option').text($(val).find('span').text());

					$.ajax({
						url: '/Admin/User/getAllReseau',
						type:'post',
						dataType:'json',
						data:{id:committeeid},
						success:function(json){
					        $.each(json,function(i,val){
					    		listr += '<div class="option" data-value="'+val.id+'" data-url=""><span>'+val.nbh_name+'</span></div>';
								$('#grid2').html(listr);
					        })
					        
					        $.each($('#grid2 .option'),function(i,val){
								if ($(val).data('value') == reseauid) {
									$(val).addClass('selected');
									$(val).siblings().removeClass('selected');
									$('#grid2').siblings('input').val(reseauid);
									$('#grid2').siblings('.show-option').text($(val).find('span').text());
								}
							})
						},
						error:function(json){

						}
					})
				}else if (committeeid == 0){
					$(val).addClass('selected');
					$(val).siblings().removeClass('selected');
					$('#neighborhood2').siblings('input').val('');
					$('#neighborhood2').siblings('.show-option').text($(val).find('span').text());
					$('#grid2').html('');
				}

				return false;
			}
		})


	})



	$('body').on('mousedown','#firstTerrace .option',function(){
		if (first_k.hasClass('selected')) {
			neighborpower.removeClass('hide');
			neighborperson.addClass('hide');

			firstPerson.attr('name','committee');
			secondPerson.attr('name','reseau');
			threePerson.attr('name','');
		}else{
			neighborpower.addClass('hide');
			neighborperson.removeClass('hide');

			threePerson.attr('name','committee');
			firstPerson.attr('name','');
			secondPerson.attr('name','');
		}
	})

	// 联动

	$('body').on('mousedown','#neighborhood2 .option',function(){
		var id = $(this).data('value');
		var listr = '';
		$('#grid2').html(listr);
		$('#grid2').siblings('input').val('');
		$('#grid2').siblings('.show-option').text('');

		$.ajax({
			url: '/Admin/User/getAllReseau',
			type:'post',
			dataType:'json',
			data:{id:id},
			success:function(json){
		        $.each(json,function(i,val){
		    		listr += '<div class="option" data-value="'+val.id+'" data-url=""><span>'+val.nbh_name+'</span></div>';
					$('#grid2').html(listr);
		        })
		        
		        
			},
			error:function(json){

			}
		})
	})

	// 提交
	secondForm.validate();
	secondForm.on('submit',function(){
		try {
			if (secondForm.valid()) {
				secondForm.submitForm();
			}

		}catch(e){
			console.log(e)
		}

		return false;
	})



	// 解冻

	$('.withdrawal').on('click',function(){
    var str = '确认解冻？';
    var id = $(this).data('id');
	    $.confirm({
	      msg:str,
	      confirm:function(){
		      $.ajax({
		          url:'/Admin/User/free',
		          type:'post',
		          data:{id:id},
		          dataType:'json',
		          success:function(json){
		            console.log(json)
		            if (json.code == 1) {
		              $.alert('解冻成功！');
		              window.location.reload();
		            }
		          },
		          error:function(json){

		          }
		        })
		    }
	    })
   })

	// 冻结

	$('.look').on('click',function(){
    var str = '冻结后该帐号无法登录APP端，是否确认冻结';
    var id = $(this).data('id');
	    $.confirm({
	      msg:str,
	      confirm:function(){
		      $.ajax({
		          url:'/Admin/User/freeze',
		          type:'post',
		          data:{id:id},
		          dataType:'json',
		          success:function(json){
		            console.log(json)
		            if (json.code == 1) {
		              $.alert('冻结成功！');
		              window.location.reload();
		            }
		          },
		          error:function(json){

		          }
		        })
		    }
	    })
   })



	// 取消权限

	$('.cancelpower').on('click',function(){
    var str = '确认取消权限？';
    var id = $(this).data('id');
	    $.confirm({
	      msg:str,
	      confirm:function(){
		      $.ajax({
		          url:'/Admin/User/revoke',
		          type:'post',
		          data:{id:id},
		          dataType:'json',
		          success:function(json){
		            console.log(json)
		            if (json.code == 1) {
		              $.alert('取消权限成功！');
		              window.location.reload();
		            }else{
		            	$.alert(json.msg);
		            }
		          },
		          error:function(json){

		          }
		        })
		    }
	    })
   })
})